home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / dflat8.zip / PICTBOX.C < prev    next >
Text File  |  1991-09-30  |  7KB  |  277 lines

  1. /* -------------- pictbox.c -------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. typedef struct    {
  6.     enum VectTypes vt;
  7.     RECT rc;
  8. } VECT;
  9.  
  10. unsigned char CharInWnd[] = "─│┌┐┘└┼├┤┴┬";
  11.  
  12. unsigned char VectCvt[3][11][2][4] = {
  13.     {    /* --- first character in collision vector --- */
  14.         /* ( drawing ─ ) ( drawing │ )       */
  15.              {{"───"},     {"┌├└"}},
  16.              {{"┌┬┐"},     {"│││"}},
  17.              {{"┌┬┬"},     {"┌├├"}},
  18.              {{"┐┐┐"},     {"┐┐┐"}},
  19.              {{"┘┘┘"},     {"┘┘┘"}},
  20.              {{"└┴┴"},     {"├├└"}},
  21.              {{"┼┼┼"},     {"┼┼┼"}},
  22.              {{"├┼┼"},     {"├├├"}},
  23.              {{"┤┤┤"},     {"┤┤┤"}},
  24.              {{"┴┴┴"},     {"┴┴┴"}},
  25.              {{"┬┬┬"},     {"┬┼┼"}}    },
  26.     {    /* --- middle character in collision vector --- */
  27.         /* ( drawing ─ ) ( drawing │ )       */
  28.              {{"───"},     {"┬┼┴"}},
  29.              {{"├┼┤"},     {"│││"}},
  30.              {{"┌┌┌"},     {"┌┌┌"}},
  31.              {{"┐┐┐"},     {"┐┐┐"}},
  32.              {{"┘┘┘"},     {"┘┘┘"}},
  33.              {{"└└└"},     {"└└└"}},
  34.              {{"┼┼┼"},     {"┼┼┼"}},
  35.              {{"├├├"},     {"├├├"}},
  36.              {{"┼┼┤"},     {"┤┤┤"}},
  37.              {{"┴┴┴"},     {"┼┼┴"}},
  38.              {{"┬┬┬"},     {"┬┬┬"}}    },
  39.     {    /* --- last character in collision vector --- */
  40.         /* ( drawing ─ ) ( drawing │ )       */
  41.              {{"───"},     {"┐┤┘"}},
  42.              {{"└┴┘"},     {"│││"}},
  43.              {{"┌┌┌"},     {"┌┌┌"}},
  44.              {{"┬┬┐"},     {"┐┤┤"}},
  45.              {{"┴┴┘"},     {"┤┤┘"}},
  46.              {{"└└└"},     {"└└└"}},
  47.              {{"┼┼┼"},     {"┼┼┼"}},
  48.              {{"├├├"},     {"├├├"}},
  49.              {{"┼┼┤"},     {"┤┤┤"}},
  50.              {{"┴┴┴"},     {"┼┼┴"}},
  51.              {{"┬┬┬"},     {"┬┬┬"}}    }
  52. };
  53.  
  54. /* -- compute whether character is first, middle, or last -- */
  55. static int FindVector(WINDOW wnd, RECT rc, int x, int y)
  56. {
  57.     RECT rcc;
  58.     VECT *vc = wnd->VectorList;
  59.     int i, coll = -1;
  60.     for (i = 0; i < wnd->VectorCount; i++)    {
  61.         if ((vc+i)->vt == VECTOR)    {
  62.             rcc = (vc+i)->rc;
  63.             /* --- skip the colliding vector --- */
  64.             if (rcc.lf == rc.lf && rcc.rt == rc.rt &&
  65.                     rcc.tp == rc.tp && rc.bt == rcc.bt)
  66.                 continue;
  67.             if (rcc.tp == rcc.bt)    {
  68.                 /* ---- horizontal vector, see if character is in it --- */
  69.                 if (rc.lf+x >= rcc.lf && rc.lf+x <= rcc.rt &&
  70.                         rc.tp+y == rcc.tp)    {
  71.                     /* --- it is --- */
  72.                     if (rc.lf+x == rcc.lf)
  73.                         coll = 0;
  74.                     else if (rc.lf+x == rcc.rt)
  75.                         coll = 2;
  76.                     else 
  77.                         coll = 1;
  78.                 }
  79.             }
  80.             else     {
  81.                 /* ---- vertical vector, see if character is in it --- */
  82.                 if (rc.tp+y >= rcc.tp && rc.tp+y <= rcc.bt &&
  83.                         rc.lf+x == rcc.lf)    {
  84.                     /* --- it is --- */
  85.                     if (rc.tp+y == rcc.tp)
  86.                         coll = 0;
  87.                     else if (rc.tp+y == rcc.bt)
  88.                         coll = 2;
  89.                     else 
  90.                         coll = 1;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     return coll;
  96. }
  97.  
  98. static void PaintVector(WINDOW wnd, RECT rc)
  99. {
  100.     int i, cw, fml, vertvect, coll, len;
  101.     unsigned int ch, nc;
  102.  
  103.     if (rc.rt == rc.lf)    {
  104.         /* ------ vertical vector ------- */
  105.         nc = '│';
  106.         vertvect = 1;
  107.         len = rc.bt-rc.tp+1;
  108.     }
  109.     else     {
  110.         /* ------ horizontal vector ------- */
  111.         nc = '─';
  112.         vertvect = 0;
  113.         len = rc.rt-rc.lf+1;
  114.     }
  115.  
  116.     for (i = 0; i < len; i++)    {
  117.         unsigned int newch = nc;
  118.         int xi = 0, yi = 0;
  119.         if (vertvect)
  120.             yi = i;
  121.         else
  122.             xi = i;
  123.         ch = videochar(GetClientLeft(wnd)+rc.lf+xi, GetClientTop(wnd)+rc.tp+yi);
  124.         for (cw = 0; cw < sizeof(CharInWnd); cw++)    {
  125.             if (ch == CharInWnd[cw])    {
  126.                 /* ---- hit another vector character ---- */
  127.                 if ((coll = FindVector(wnd, rc, xi, yi)) != -1)    {
  128.                     /* compute first/middle/last subscript */
  129.                     if (i == len-1)
  130.                         fml = 2;
  131.                     else if (i == 0)
  132.                         fml = 0;
  133.                     else
  134.                         fml = 1;
  135.                     newch = VectCvt[coll][cw][vertvect][fml];
  136.                 }
  137.             }
  138.         }
  139.         PutWindowChar(wnd, newch, rc.lf+xi, rc.tp+yi);
  140.     }
  141. }
  142.  
  143. static void PaintBar(WINDOW wnd, RECT rc, enum VectTypes vt)
  144. {
  145.     int i, vertbar, len;
  146.     unsigned int tys[] = {'█', '▓', '▒', '░'};
  147.     unsigned int nc = tys[vt-1];
  148.  
  149.     if (rc.rt == rc.lf)    {
  150.         /* ------ vertical bar ------- */
  151.         vertbar = 1;
  152.         len = rc.bt-rc.tp+1;
  153.     }
  154.     else     {
  155.         /* ------ horizontal bar ------- */
  156.         vertbar = 0;
  157.         len = rc.rt-rc.lf+1;
  158.     }
  159.  
  160.     for (i = 0; i < len; i++)    {
  161.         int xi = 0, yi = 0;
  162.         if (vertbar)
  163.             yi = i;
  164.         else
  165.             xi = i;
  166.         PutWindowChar(wnd, nc, rc.lf+xi, rc.tp+yi);
  167.     }
  168. }
  169.  
  170. static void PaintMsg(WINDOW wnd)
  171. {
  172.     int i;
  173.     VECT *vc = wnd->VectorList;
  174.     for (i = 0; i < wnd->VectorCount; i++)    {
  175.         if (vc->vt == VECTOR)
  176.             PaintVector(wnd, vc->rc);
  177.         else
  178.             PaintBar(wnd, vc->rc, vc->vt);
  179.         vc++;
  180.     }
  181. }
  182.  
  183. static void DrawVectorMsg(WINDOW wnd, PARAM p1, enum VectTypes vt)
  184. {
  185.     if (p1)    {
  186.         wnd->VectorList = realloc(wnd->VectorList,
  187.                 sizeof(VECT) * (wnd->VectorCount + 1));
  188.         if (wnd->VectorList != NULL)    {
  189.             VECT vc;
  190.             vc.vt = vt;
  191.             vc.rc = *(RECT *)p1;
  192.             *(((VECT *)(wnd->VectorList))+wnd->VectorCount) = vc;
  193.             wnd->VectorCount++;
  194.         }
  195.     }
  196. }
  197.  
  198. static void DrawBoxMsg(WINDOW wnd, PARAM p1)
  199. {
  200.     if (p1)    {
  201.         RECT rc = *(RECT *)p1;
  202.         rc.bt = rc.tp;
  203.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, TRUE);
  204.         rc = *(RECT *)p1;
  205.         rc.lf = rc.rt;
  206.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, FALSE);
  207.         rc = *(RECT *)p1;
  208.         rc.tp = rc.bt;
  209.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, TRUE);
  210.         rc = *(RECT *)p1;
  211.         rc.rt = rc.lf;
  212.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, FALSE);
  213.     }
  214. }
  215.  
  216. int PictureProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  217. {
  218.     switch (msg)    {
  219.         case PAINT:
  220.             BaseWndProc(PICTUREBOX, wnd, msg, p1, p2);
  221.             PaintMsg(wnd);
  222.             return TRUE;
  223.         case DRAWVECTOR:
  224.             DrawVectorMsg(wnd, p1, VECTOR);
  225.             return TRUE;
  226.         case DRAWBOX:
  227.             DrawBoxMsg(wnd, p1);
  228.             return TRUE;
  229.         case DRAWBAR:
  230.             DrawVectorMsg(wnd, p1, p2);
  231.             return TRUE;
  232.         case CLOSE_WINDOW:
  233.             if (wnd->VectorList != NULL)
  234.                 free(wnd->VectorList);
  235.             break;
  236.         default:
  237.             break;
  238.     }
  239.     return BaseWndProc(PICTUREBOX, wnd, msg, p1, p2);
  240. }
  241.  
  242. static RECT PictureRect(int x, int y, int len, int hv)
  243. {
  244.     RECT rc;
  245.     rc.lf = rc.rt = x;
  246.     rc.tp = rc.bt = y;
  247.     if (hv)
  248.         /* ---- horizontal vector ---- */
  249.         rc.rt += len-1;
  250.     else
  251.         /* ---- vertical vector ---- */
  252.         rc.bt += len-1;
  253.     return rc;
  254. }
  255.  
  256. void DrawVector(WINDOW wnd, int x, int y, int len, int hv)
  257. {
  258.     RECT rc = PictureRect(x,y,len,hv);
  259.     SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, 0);
  260. }
  261.  
  262. void DrawBox(WINDOW wnd, int x, int y, int ht, int wd)
  263. {
  264.     RECT rc;
  265.     rc.lf = x;
  266.     rc.tp = y;
  267.     rc.rt = x+wd-1;
  268.     rc.bt = y+ht-1;
  269.     SendMessage(wnd, DRAWBOX, (PARAM) &rc, 0);
  270. }
  271.  
  272. void DrawBar(WINDOW wnd, enum VectTypes vt, int x, int y, int len, int hv)
  273. {
  274.     RECT rc = PictureRect(x,y,len,hv);
  275.     SendMessage(wnd, DRAWBAR, (PARAM) &rc, (PARAM) vt);
  276. }
  277.